
package com.shop.cloud.mall.api.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.cloud.common.core.constant.CacheConstants;
import com.shop.cloud.common.data.tenant.TenantContextHolder;
import com.shop.cloud.common.portal.util.ThirdSessionHolder;
import com.shop.cloud.mall.common.entity.ShoppingCart;
import com.shop.cloud.mall.api.mapper.ShoppingCartMapper;
import com.shop.cloud.mall.api.service.ShoppingCartService;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 购物车
 *
 * @author
 * @date 2019-08-29 21:27:33
 */
@Service
@AllArgsConstructor
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

	private final RedisTemplate redisTemplate;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean save(ShoppingCart entity) {
		ShoppingCart shoppingCart = baseMapper.selectOne(Wrappers.<ShoppingCart>lambdaQuery()
				.eq(ShoppingCart::getUserId,entity.getUserId())
				.eq(ShoppingCart::getSpuId,entity.getSpuId())
				.eq(ShoppingCart::getSkuId,entity.getSkuId()));
		if(shoppingCart != null){
			entity.setQuantity(entity.getQuantity() + shoppingCart.getQuantity());
			baseMapper.deleteById(shoppingCart);
		}
		super.save(entity);
		//清缓存
		redisTemplate.delete(getRedisKey());
		return true;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean updateById(ShoppingCart entity) {
		if(StrUtil.isAllNotEmpty(entity.getSpuId(), entity.getSkuId())){
			ShoppingCart shoppingCart = baseMapper.selectOne(Wrappers.<ShoppingCart>lambdaQuery()
					.eq(ShoppingCart::getUserId,entity.getUserId())
					.eq(ShoppingCart::getSpuId,entity.getSpuId())
					.eq(ShoppingCart::getSkuId,entity.getSkuId()));
			if(shoppingCart != null && !entity.getId().equals(shoppingCart.getId())){
				entity.setQuantity(entity.getQuantity() + shoppingCart.getQuantity());
				baseMapper.deleteById(shoppingCart);
			}
		}
		super.updateById(entity);
		//清缓存
		redisTemplate.delete(getRedisKey());
		return true;
	}

	@Override
	public List<ShoppingCart> list2(ShoppingCart shoppingCart) {
		String key = getRedisKey();
		List<ShoppingCart> listShoppingCart = new ArrayList<>();
		if(redisTemplate.hasKey(key)) {
			Object obj = redisTemplate.opsForValue().get(key);
			if(obj != null){
				listShoppingCart = JSONUtil.toList(JSONUtil.parseArray(obj), ShoppingCart.class);
			}
		}else {
			listShoppingCart = baseMapper.selectList2(shoppingCart);
			//存入缓存
			String data = JSONUtil.toJsonStr(listShoppingCart);
			redisTemplate.opsForValue().set(key, data,5, TimeUnit.DAYS);
		}
		return listShoppingCart;
	}

	@Override
	public boolean removeByIds(Collection<?> idList) {
		super.removeByIds(idList);
		//清缓存
		redisTemplate.delete(getRedisKey());
		return true;
	}

	@Override
	public boolean remove(Wrapper<ShoppingCart> queryWrapper) {
		super.remove(queryWrapper);
		//清除用户购物车缓存
		String redisShoppingCartKey = getRedisKey();
		redisTemplate.delete(redisShoppingCartKey);
		return true;
	}

	String getRedisKey(){
		return StrUtil.format("{}:{}::{}", TenantContextHolder.getTenantId(), CacheConstants.MALL_SHOPPINGCART_CACHE, ThirdSessionHolder.getUserId());
	}
}
